package com.yjs.app.trade.core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类
 * 
 * @author hikaru
 * @since 1.0.0
 */
public class DateUtil {

	public static DateFormat dateFormatMonth = new SimpleDateFormat("yyyy-MM");
	
	public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	public static DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

	public static DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static DateFormat getDateFormat() {
		return dateFormat;
	}

	public static DateFormat getTimeFormat() {
		return timeFormat;
	}

	public static DateFormat getDateTimeFormat() {
		return dateTimeFormat;
	}

	/**
	 * 计算时长
	 * 
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @param type 时长类型（Calendar.SECOND-秒，Calendar.MINUTE-分，Calendar.HOUR-时）
	 */
	public static long getDuration(Date begin, Date end, int type) {
		if (begin == null || end == null) {
			throw new IllegalArgumentException("begin date or end date is null");
		}
		long duration = end.getTime() - begin.getTime();
		if (duration > 0) {
			if (type == Calendar.SECOND) {
				return duration / 1000;
			} else if (type == Calendar.MINUTE) {
				return duration / 1000 / 60;
			} else if (type == Calendar.HOUR) {
				return duration / 1000 / 60 / 60;
			} else {
				throw new IllegalArgumentException("wrong type:" + type);
			}
		} else {
			throw new IllegalArgumentException("sign off date can not be before sign in date");
		}
	}
	
	/**
	 * 获取日期当天开始时间
	 */
	public static Date getMinOfDate(Date date) {
		if (date == null) {
			return null;
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
			calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
			calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
			return calendar.getTime();
		}
	}
	
	/**
	 * 获取日期当天结束时间
	 */
	public static Date getMaxOfDate(Date date) {
		if (date == null) {
			return null;
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
			calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
			calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
			return calendar.getTime();
		}
	}
	
	public static boolean isDateFormart (String date) {
		try {
			DateUtil.dateFormat.parse(date);
			return true;
		} catch (ParseException e) {
			return false;
		}
	}

	/**
	  * 等待时间
	  * @param minutes
	  * @param hour
	  * @throws InterruptedException
	  */
	 public static void threedloop(int minutes,int hour) throws InterruptedException {
		Calendar calendar = Calendar.getInstance();
		while (true) {
			calendar.setTime(new Date());
			if (calendar.get(Calendar.SECOND) == 0) {
				break;
			} else {
				Thread.sleep(1000);
			}
		}
		while (true) {
			calendar.setTime(new Date());
			if (calendar.get(Calendar.SECOND) == 59) {
				Thread.sleep(1000);
			}
			if (calendar.get(Calendar.SECOND) == 1) {
				Thread.sleep(59000);
			}
			if (calendar.get(Calendar.MINUTE) == minutes && calendar.get(Calendar.HOUR_OF_DAY) == hour) {
				break;
			} else {
				Thread.sleep(1000 * 60);
			} 
		}
	 }
	 
	 /**
	  * 获取几天前的时间
	  */
	 public static Date getDateBeforeDay(Date date , int day) {
		 Calendar calendar = Calendar.getInstance(); 
		 calendar.setTime(date);
		 calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - day);
		 return calendar.getTime();
	 }
	 
	 /**
	  * 获取2个时间差
	  * @param beginDt
	  * @param endDt
	  * @return
	  */
	 public static int getDateBetween (Date beginDt , Date endDt) {
		 return (int) ((endDt.getTime() - beginDt.getTime())/(1000 * 60 * 60 * 24));
	 }
	 
	 /**
	  * 比较2个时间大小
	  * @param DATE1
	  * @param DATE2
	  * @return
	  */
	 public static int compare_date(Date DATE1, Date DATE2) {
		 if (DATE1.getTime() > DATE2.getTime()) {
	            return 1;
	     } else if (DATE1.getTime() < DATE2.getTime()) {
	            return -1;
	     } else {
	            return 0;
	     }
     }
	 
	 /**
	  * 获取时间小时
	  * @param date
	  * @return
	  */
	 public static int getHourForData (Date date) {
		 Calendar calendar = Calendar.getInstance();
		 calendar.setTime(date);
		 return calendar.get(Calendar.HOUR);
	 }
	 
	 /**
	  * 获取时间分钟数
	  * @param date
	  * @return
	  */
	 public static int getMinuteForData (Date date) {
		 Calendar calendar = Calendar.getInstance();
		 calendar.setTime(date);
		 return calendar.get(Calendar.MINUTE);
	 }
	 
	 /**
	  * 获取多少时间前的所有时间
	  * @param date
	  * @param num
	  * @param type
	  * @return
	  */
	 public static List<String> getLastDates(Date date, int num, int type, DateFormat formatType) {
		 List<String> list = new ArrayList<String>();
		 for (int i = 1; i <= num; i++) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(type, -i);
			list.add(formatType.format(calendar.getTime()));
		 }
		 return list;
	 }
} 
